package com.timi_licai.timi_licai_account.dialog;

import android.app.Dialog;
import android.content.ContentValues;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.timi_licai.timi_licai_account.MainActivity;
import com.timi_licai.timi_licai_account.MyConstants;
import com.timi_licai.timi_licai_account.R;
import com.timi_licai.timi_licai_account.database.DatabaseUtils;
import com.timi_licai.timi_licai_account.listener.EditBookInterface;

/**
 * Created by LGH on 2015/3/19.
 */
public class EditBookDialog extends Dialog implements View.OnClickListener {

    private EditText editBook;
    private EditBookInterface bookListener;
    private long position;
    private Context context;

    public void setBookListener (EditBookInterface bookListener) {
        this.bookListener = bookListener;
    }

    public EditBookDialog (Context context) {
        super(context);
    }

    public EditBookDialog (Context context, int theme, long position) {
        super(context, theme);
        this.position = position;
        this.context = context;
    }

    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_updata_book);
        editBook = (EditText) findViewById(R.id.update_book_edit);
        TextView txtEnsure = (TextView) findViewById(R.id.update_book_ensure);
        TextView txtDelete = (TextView) findViewById(R.id.update_book_delete);
        txtDelete.setOnClickListener(this);
        txtEnsure.setOnClickListener(this);
    }

    @Override
    public void onClick (View v) {
        int id = v.getId();
        switch (id){
            case R.id.update_book_delete:
                // TODO 删除当前账本
                deleteBook();
                dismiss();
                if (context instanceof MainActivity){
                    MainActivity activity = (MainActivity)context;
                    activity.flushCountBook();
                }
                break;
            case R.id.update_book_ensure:
                // TODO 确定修改当前账本名称

                String bookName = editBook.getText().toString().trim();
                if (!bookName.equals("")) {
                    if (bookListener != null) {
                        bookListener.editBookClick(bookName);
                        updateBookName(bookName);
                    }
                }else {
                    dismiss();
                    break;
                }
                dismiss();
                if (context instanceof MainActivity){
                    MainActivity activity = (MainActivity)context;
                    activity.flushCountBook();
                }
                break;
        }
    }

    /**
     *  编辑账本名称
     */
    private void updateBookName(String name){
        ContentValues values = new ContentValues();
        values.put("bookname", name);
        DatabaseUtils.update(MyConstants.TABLE_ACCOUNT_BOOK, values,
                "_id=?", new String[] {position+""});
    }

    private void deleteBook(){
        DatabaseUtils.delete(MyConstants.TABLE_ACCOUNT_BOOK,"_id=?", new String[] {position+""});
    }
}
